import { maccApi, requestHttp } from '@utils/index';
export const localRouter = (layout) => {
  return [
    {
      path: '/',
      name: 'index',
      component: 'authSys/views/authDashboard/index.vue',
      meta: {
        requireAuth: true,
        layout: layout,
      },
    },
    // 主页
    {
      path: '/authHome',
      name: 'authHome',
      component: 'authSys/views/authDashboard/index.vue',
      meta: {
        requireAuth: true,
        layout: layout,
      },
    },
    // portal用户
    {
      path: '/authClient',
      name: 'authClient',
      component: 'authSys/views/authClient/index.vue',
      meta: {
        requireAuth: true,
        layout: 'BOTH',
      },
    },
    // 认证日志
    {
      path: '/authLog',
      name: 'authLog',
      component: 'authSys/views/authLog/index.vue',
      meta: {
        requireAuth: true,
        layout: 'BOTH',
      },
    },
    // 异常日志
    {
      path: '/eventViewer',
      name: 'eventViewer',
      component: 'authSys/views/eventViewer/index.vue',
      meta: {
        requireAuth: true,
        layout: 'BOTH',
      },
    },
    // 异常日志
    {
      path: '/debugLog',
      name: 'debugLog',
      component: 'authSys/views/debugLog/index.vue',
      meta: {
        requireAuth: true,
        layout: 'BOTH',
      },
    },
    {
      path: '/sysSetting',
      name: 'sysSetting',
      component: 'About.vue', // 海外首页直接用新首页
      meta: {
        requireAuth: true,
        layout: layout,
      },
    },
    {
      // 快速创建
      path: '/quickCreate',
      name: 'quickCreate',
      component: 'authSys/views/quickCreate/index.vue',
      meta: {
        requireAuth: true,
        layout: 'BOTH',
      },
    },
    {
      // 规则列表
      path: '/ruleList',
      name: 'ruleList',
      component: 'authSys/views/ruleList/index.vue',
      meta: {
        requireAuth: true,
        layout: 'BOTH',
      },
    },
    {
      // 设备管理
      path: '/authDevice',
      name: 'authDevice',
      component: 'authSys/views/authDevice/index.vue',
      meta: {
        requireAuth: true,
        layout: 'BOTH',
      },
    },
    {
      // 本地账号
      path: '/localAccount',
      name: 'localAccount',
      component: 'authSys/views/localAccount/index.vue',
      meta: {
        requireAuth: true,
        layout: 'BOTH',
      },
    },
    {
      // Voucher
      path: '/authVoucher',
      name: 'authVoucher',
      component: 'authSys/views/authVoucher/index.vue',
      meta: {
        requireAuth: true,
        layout: 'BOTH',
      },
    },
    {
      //voucher打印页面
      path: '/voucherPrint',
      name: 'voucherPrint',
      component: 'authSys/views/authVoucher/printPage.vue',
      meta: {
        requireAuth: true,
        layout: 'BOTH',
      },
    },
    {
      // MAC管理
      path: '/authMac',
      name: 'authMac',
      component: 'authSys/views/authMac/index.vue',
      meta: {
        requireAuth: true,
        layout: 'BOTH',
      },
    },
    {
      // 第三方身份源
      path: '/thirdPart',
      name: 'thirdPart',
      component: 'authSys/views/thirdPart/index.vue',
      meta: {
        requireAuth: true,
        layout: 'BOTH',
      },
    },
    {
      // 授权策略
      path: '/authPolicy',
      name: 'authPolicy',
      component: 'authSys/views/authPolicy/index.vue',
      meta: {
        requireAuth: true,
        layout: 'BOTH',
      },
    },

    // 账户设置
    {
      path: '/accountSet',
      name: 'AccountSet',
      component: 'system/usercenter/index.vue',
      meta: {
        requireAuth: true,
        layout: 'TOP_MENU',
      },
    },
    {
      path: '/license',
      name: 'license',
      component: 'authSys/views/licenseManage/index.vue',
      meta: {
        requireAuth: true,
        layout: 'BOTH',
      },
    },
    {
      path: '/certificate',
      name: 'certificate',
      component: 'authSys/views/certificate/index.vue',
      meta: {
        requireAuth: true,
        layout: 'BOTH',
      },
    },
    {
      path: '/twilioSms',
      name: 'twilioSms',
      component: 'authSys/views/twilioSms/index.vue',
      meta: {
        requireAuth: true,
        layout: 'BOTH',
      },
    },
    {
      path: '/systemMonitor',
      name: 'systemMonitor',
      component: 'authSys/views/systemMonitor/index.vue',
      meta: {
        requireAuth: true,
        layout: 'BOTH',
      },
    },
    // 海外子账号
    {
      path: '/subAccount',
      name: 'subAccount',
      component: 'authSys/views/subAccount/index.vue',
      meta: {
        requireAuth: true,
        layout: 'TOP_MENU',
      },
    },
    {
      // 快速创建
      path: '/faultCollect',
      name: 'faultCollect',
      component: 'authSys/views/faultCollect/index.vue',
      meta: {
        requireAuth: true,
        layout: 'BOTH',
      },
    },
  ];
};

export const getMenu = async (theme) => {
  let api = '';
  // switch (theme) {
  //   case 'NOC': {
  //     api = maccApi.SYSTEM_NOCMENUTREE_GET_API;
  //     break;
  //   }
  //   case 'RUIYI': {
  //     api = maccApi.SYSTEM_MENUTREE_GET_API.replace('{scene}', 'MACC5');
  //     break;
  //   }
  //   case 'RUIYI_V2': {
  api = maccApi.SYSTEM_MENUTREE_GET_API.replace('{scene}', 'REYEE');
  //     break;
  //   }
  // }
  const { moduleTree = [], scene } = await requestHttp({ api, method: 'GET' });
  return { moduleTree, scene };
};

/**
 * 菜单和本地路由映射
 * @returns
 */
export const getMenuMap = () => {
  return {
    authHome_menu: '/authHome',

    authMonitor_authLogList_menu: '/authLog',
    authMonitor_eventViewer_menu: '/eventViewer',
    authMonitor_debugLog_menu: '/debugLog',
    authMonitor_portalAuthClient_menu: '/authClient',

    authConf_quickCreate_menu: '/quickCreate',
    authConf_ruleList_menu: '/ruleList',
    authConf_authDevice_menu: '/authDevice',
    authConf_localAccount_menu: '/localAccount',
    authConf_authVoucher_menu: '/authVoucher',
    authConf_authMac_menu: '/authMac',
    authConf_thirdPart_menu: '/thirdPart',
    authConf_authPolicy_menu: '/authPolicy',

    sysSetting_menu: '/sysSetting',
    sysSetting_sysMonitoring_menu: '/systemMonitor',
    sysSetting_certificate_menu: '/certificate',

    sysSetting_licenseMng_menu: '/license',
    sysSetting_twilioSms_menu: '/twilioSms',
    sysSetting_faultCollect_menu: '/faultCollect',

    private_accountsetting_menu: '/accountSet', //账号设置
    private_subAccount_menu: '/subAccount',
  };
};

/**
 * 变更路由
 * @param {*} menuData
 * @param {*} localRouter
 */
export const formatterMenu = (
  menuData,
  localRouter,
  commit,
  currentPath = ''
) => {
  const menuMap = getMenuMap();

  for (let i = 0; i < menuData.length; i++) {
    const menu = menuData[i];
    const pathStr = currentPath + menu.moduleId + ',';
    const menuId = menu.moduleId.replace(/\//g, '_') + '_menu';
    if (menuMap[menuId]) {
      localRouter.forEach((item) => {
        if (menuMap[menuId] == item.path) {
          //if (item.name != 'Index') {
          item.path = `/${menuId}`;
          item.meta.label = menu.label;
          item.meta.menuDesc = menu.name;
          //}
          commit('SET_USER_MENUPATH', {
            key: item.path,
            path: pathStr.split(','),
          });
        }
      });
    }
    if (menu.children && menu.children.length > 0) {
      formatterMenu(menu.children, localRouter, commit, pathStr);
    }
  }
};
